<%@ page import="java.net.URLEncoder"%>
<%@ page import="java.io.*" %>
<%@ page import="java.util.LinkedList"%>
<%@ page import="java.util.HashMap"%>
<%@ page import="java.util.HashSet"%>
<%@ page import="java.util.Map"%>
<%@ page import="java.util.Map.Entry"%>
<%@ page import="java.util.Set"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="javax.xml.parsers.DocumentBuilderFactory,javax.xml.parsers.DocumentBuilder,
				 org.w3c.dom.*"%>
<%@ page import="java.sql.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<jsp:useBean id="db" class="ctm.javacode.dbBean"/>
<jsp:useBean id="timeS" class="ctm.javacode.lib"/>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>中醫體質評量系統</title>

<style type="text/css">
<!--
body {
	background-image: url(img/grass.jpg);
    background-position: center top;
	background-color: #FFFFFF;
	background-repeat: repeat;
}
body,td,th {
	color: #000000;
	font-size:24px;
	font-family: Arial;
}



a:link {
text-decoration:none;
font-family:Arial;
font-size:20px;
color:#FFFFFF;
}
a:visited {
text-decoration:none;
font-family:Arial;
font-size:20px;
color:#FFFFFF;
}
a:hover {
text-decoration:none;
font-family:Arial;
font-size:20px;
color:#000000;
}


.ind:link {
color: #535353;
text-decoration:none;
font-family:"標楷體";
font-size:18px;
}
.ind:visited {
color:#535353;
text-decoration:none;
font-family:"標楷體";
font-size:18px;
}

.ind:hover {
color:#BB280F;
text-decoration:none;
font-family:"標楷體";
font-size:18px;
}

 .off { display:none;}
 .on  { display:block;}

.HEAD_STYLE {
	font-weight:bold;
	color: #437A27;
	font-family:Arial;
	font-size:30px; 
}
.TEXT_STYLE {
	font-family:"標楷體";  
	color:#437A27;
	font-size:24px;
}
.TEXT_STYLE_2 {
	font-family:"標楷體";  
	color:#000000;
	font-size:24px;
}
.BOTTOM_STYLE {
	color: #FFFFFF; 
	font-family: Arial; 
	font-size:20px;
}
.LEFT_BUTTON {
	font-family:"標楷體"; 
	font-size:30px;
}
.button
{
	border:1px;
	padding-top: 4px;
	padding-right: 7px;
	padding-bottom: 4px;
	padding-left: 7px;
	background-color: #468129;
	color: #FFFFFF;
	font-size: 16px;
	border-color: #3C7023;
}

-->
</style>

<!--  -->
<script language="Javascript" type="text/javascript">
function showVideo(ss){
	var str = "showVedio.jsp?source="+ss+"";
	var str4 = "newwindow";
	var str5 ="width=800,height=700, toolbar =no, menubar=no, scrollbars=no, resizable=no, location=no, status=no";
window.open(str,str4,str5);
}

function see(name_1 , name_2 , name_3, name_4){
	
	if(document.getElementById(name_1).className== "on"){
		document.getElementById(name_1).className = "off";
		document.getElementById(name_3).className = "off";
		document.getElementById("change").value ="觀看適宜的運動" ; 
	}
	else{
		document.getElementById(name_1).className = "on";
		document.getElementById(name_3).className = "on";
		document.getElementById("change").value ="觀看運動總表" ;
	}
	
	
	if(document.getElementById(name_2).className== "on"){
		document.getElementById(name_2).className = "off";
		document.getElementById(name_4).className = "off";
	}
	else{
		document.getElementById(name_2).className = "on";
		document.getElementById(name_4).className = "on";
	}
		
}
</script>
</head>
<body alink="#FF0000" vlink="#FF0000">


<%	
	db.setDataSource("jdbc/ctmpllab");
	db.Connected();
	String name = (String)request.getSession().getAttribute("login");
	String filename ="";
	String sqlQuery;
	if (request.isUserInRole("admin")|| request.isUserInRole("professional")) {
		sqlQuery = "select testTime from questionTable where account = '" + name + "'&&identity='nopersonId'order by id desc"; //nopersonId為未輸入身分證字號
	}else{
		sqlQuery = "select testTime from questionTable where account = '" + name + "'order by id desc";
	}
	ResultSet rs;
	rs = db.ExcuteQuery(sqlQuery);	
	rs.first();
	
%>

<%!
  public boolean isTextNode(Node n){
  return n.getNodeName().equals("#text");
  }

//2個字串取交集  
public static String[] intersect (String[] arr1, String[] arr2) {  
    Map<String, Boolean> map = new HashMap<String, Boolean>();  
    LinkedList<String> list = new LinkedList<String>();  
    for (String str : arr1) {  
        if (!map.containsKey(str)) {  
            map.put(str, Boolean.FALSE);  
        }  
    }  
    for (String str : arr2) {  
        if (map.containsKey(str)) {  
            map.put(str, Boolean.TRUE);  
        }  
    }  

    for (Entry<String, Boolean> e : map.entrySet()) {  
        if (e.getValue().equals(Boolean.TRUE)) {  
            list.add(e.getKey());  
        }  
    }  

    String[] result = {};  
    return list.toArray(result);  
}  
%>

<table width="100%" border="0">
		
	<tr>
		<td><p>&nbsp;</p></td>
	</tr>
	<tr>					
		<td width="5%">&nbsp;</td>
		<td bgcolor="#FFFFFF">
			
			<table width="100%" border="0">
				<tr>
					<td background="img/herbal.jpg">
						<p>&nbsp;</p>
						<p>&nbsp;</p>
						<p>&nbsp;</p>
						<p>&nbsp;</p>
						<p>&nbsp;</p>
					
					</td>
				</tr>
				<tr>
					<td>	
						<table bgcolor="#437A27" width="100%" border="0">
							<tr>
								<td>&nbsp;</td>
								<td width="20%" align="center"><a href="main.jsp" target="_parent">回到主選單</a></td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
					<td>
						<table width="100%">
							<tr>
								<td valign="top">
									<table width="100%">
										<tr>
											<td class="HEAD_STYLE" align="center">
											<% if(rs.getRow()==0){%> 運動總表<%} else{%> 您適宜的運動 <%}%>
											</td>
										</tr>
										<tr>
											<td >

<table width="80%"align="center" border="1">
<!----------------------------使用者若沒有填過體質問券，會出現運動總表--------------------------------------------------------------------------------------> 													
<% if(rs.getRow()==0){ %>

<% //讀取運動xml檔
String File = request.getRealPath("./WEB-INF/sport.xml");
Document doc = null;
DocumentBuilderFactory docFactory  = DocumentBuilderFactory.newInstance();

try{
DocumentBuilder docBuilder  = docFactory.newDocumentBuilder();
doc = docBuilder.parse(File);

}catch(Exception e){
out.print("XML analysis error" + e.getMessage());
}
%>
<%
Element  Sport = doc.getDocumentElement(); 
NodeList TypeNode = Sport.getElementsByTagName("太極拳");
Node 	   TaiJi = TypeNode.item(0);
NodeList MethodNodes = TaiJi.getChildNodes();
%>

<!----------------------------運動總表------------------------------------------------------->
<tr><td><table width="100%" border="1" align="center">
<% 
NodeList TypeNode_1 = Sport.getElementsByTagName("其他運動");
Node 	   OtherSport = TypeNode_1.item(0);
NodeList SportList = OtherSport.getChildNodes();
%>

<%
for(int x=0;x<SportList.getLength();x++){ 
Node List = SportList.item(x);
if (isTextNode(List))
continue;
%>
<tr>
	<td  width="25%" class="TEXT_STYLE"><%= List.getNodeName() %></td>
	<td class="TEXT_STYLE_2"><%= List.getFirstChild().getNodeValue() %></td>	
</tr>

<% } %>
</table></td></tr>
<!---------------------------太極拳總表------------------------------------------------------>
<tr><td><table width="100%" border="1" align="center">
<tr><td style="border:hidden;" class="TEXT_STYLE_2"><%= TaiJi.getNodeName() %>(示範影片)</td></tr>
<% for(int i=0;i<MethodNodes.getLength();i++){ 
Node Method = MethodNodes.item(i);
if (isTextNode(Method))
continue;
%>
<tr><td   class="TEXT_STYLE"><%= Method.getNodeName() %></td>

<%
NodeList  SizeNodes= Method.getChildNodes();
for(int j=0;j<SizeNodes.getLength();j++){

Node Size = SizeNodes.item(j);
if (isTextNode(Size))
continue;
%>

<td><a name="testa" href="javascript:showVideo('<%= Size.getAttributes().getNamedItem("file").getNodeValue() %>')" class="ind">
<%= Size.getFirstChild().getNodeValue() %>
</a>	
<% }
%></td></tr>



<% } %>
</table></td></tr>
<!----------------------------使用者若已填過體質問券，會出根據最新一份體質結果挑選適合的運動-----------------------------------------------------------------> 		
<% } else{ %>

<%
filename=rs.getString("testTime");
String loginName = (request.getSession().getAttribute("login")).toString();
LinkedList<String> typeForPrint = new LinkedList<String>(); 
%>

<%//讀取使用者最新歷史問卷的xml
Document history = null;
File file = new File("C:/history/general/"+loginName+"/"+filename+".xml"); 
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try{
DocumentBuilder builder = factory.newDocumentBuilder();
history = builder.parse(file);

}catch(Exception e){
out.print("XML analysis error" + e.getMessage());
}
%>

<%
Element  Body = history.getDocumentElement(); 
NodeList TypeNode_t = Body.getElementsByTagName("typePrint");
Node 	  Type = TypeNode_t.item(0);
NodeList NumberNode = Type.getChildNodes();
%>

<%

for(int n=0;n<NumberNode.getLength();n++){

Node Number = NumberNode.item(n);
if (isTextNode(Number))
continue;

//儲存使用者的體質
if(Number.hasChildNodes()){

if(Number.getFirstChild().getNodeValue().contains("基本是")){
typeForPrint.add(Number.getFirstChild().getNodeValue().substring(3));	
}
else if (Number.getFirstChild().getNodeValue().contains("傾向")){
	typeForPrint.add(Number.getFirstChild().getNodeValue().substring(2));	
	}
else{
typeForPrint.add(Number.getFirstChild().getNodeValue());	
}

}

}

%>   	

<% //讀取運動xml檔
String File = request.getRealPath("./WEB-INF/sport.xml");
Document doc = null;
DocumentBuilderFactory docFactory  = DocumentBuilderFactory.newInstance();

try{
DocumentBuilder docBuilder  = docFactory.newDocumentBuilder();
doc = docBuilder.parse(File);

}catch(Exception e){
out.print("XML analysis error" + e.getMessage());
}

%>

<%
Element  Sport = doc.getDocumentElement(); 
NodeList ConstitutionNode = Sport.getElementsByTagName("太極拳功法程度");
Node 	  Constitution = ConstitutionNode.item(0);
NodeList TypeOfConstitution = Constitution.getChildNodes();

NodeList TypeNode_1 = Sport.getElementsByTagName("其他運動");
Node 	   OtherSport = TypeNode_1.item(0);
NodeList SportList = OtherSport.getChildNodes();
%>

<%
NodeList TypeNode = Sport.getElementsByTagName("太極拳");
Node 	   TaiJi = TypeNode.item(0);
NodeList MethodNodes = TaiJi.getChildNodes();
%>

<% //儲存使用者所擁有體質所需的功法程度
LinkedList<String> temp = new LinkedList<String>();

for(int k=0;k<TypeOfConstitution.getLength() ;k++){

Node NumOfConstitution = TypeOfConstitution.item(k);

if (isTextNode(NumOfConstitution))

continue;


for(int j=0;j<typeForPrint.size();j++){

if(NumOfConstitution.getNodeName().equals(typeForPrint.get(j))){

temp.add(NumOfConstitution.getFirstChild().getNodeValue());
%>

<%

}
}

%>

<% } %>

<% //儲存使用者所擁有體質所需的運動
LinkedList<String> temp_2 = new LinkedList<String>();

for(int k=0;k<SportList.getLength() ;k++){

Node NumOfSportList = SportList.item(k);

if (isTextNode(NumOfSportList))

continue;


for(int j=0;j<typeForPrint.size();j++){

if(NumOfSportList.getNodeName().equals(typeForPrint.get(j))){

	temp_2.add(NumOfSportList.getFirstChild().getNodeValue());
%>

<%

}
}

%>

<% } %>


<!----------------------適宜運動列表---------------------------------------------------------->
<tr id="List_O" class="on" ><td><table width="100%" border="1" align="center" >
<% //把使用者所擁有體質的運動取交集
String[] result_insect_2=temp_2.getFirst().split(","); 

for(int m=1;m<temp_2.size();m++){		

String[]	arr1=temp_2.get(m).split(",");

result_insect_2 = intersect(result_insect_2 , arr1); 

}  %>
<tr>
<%	for (String str : result_insect_2) {  %>
		
	 <td class="TEXT_STYLE_2"><%= str %></td>	
<%} %>
</tr>

</table></td></tr>

<!---------------------運動總表------------------------------------------------------------------->
<tr id="AllList_O" class="off"><td><table width="100%" border="1" align="center">


<%
for(int x=0;x<SportList.getLength();x++){ 
Node List = SportList.item(x);
if (isTextNode(List))
continue;
%>
<tr>
	<td   class="TEXT_STYLE" width="25%"><%= List.getNodeName() %></td>
	<td class="TEXT_STYLE_2"><%= List.getFirstChild().getNodeValue() %></td>	
</tr>

<% } %>
</table></td></tr>

<tr><td style="border:hidden;" class="TEXT_STYLE_2">
<%= TaiJi.getNodeName() %>(示範影片)
</td></tr>
<!---------------------適宜太極拳列表------------------------------------------------------------------->
<% //把使用者所擁有體質的太極拳功法大小取交集
String[] result_insect=temp.getFirst().split(","); 

for(int m=1;m<temp.size();m++){		

String[]	arr1=temp.get(m).split(",");

result_insect = intersect(result_insect , arr1); 

}  %>

<tr id="List_T" class="on"><td><table width="100%" border="1" align="center">
<% for(int i=0;i<MethodNodes.getLength();i++){ 
Node Method = MethodNodes.item(i);
if (isTextNode(Method))
continue;
%>
 
<tr><td   class="TEXT_STYLE"><%= Method.getNodeName() %></td>

<%
NodeList  SizeNodes= Method.getChildNodes();

for(int j=0;j<SizeNodes.getLength();j++){

Node Size = SizeNodes.item(j);
if (isTextNode(Size)  )
continue;
%>

<%	for (String str : result_insect) {  %>

<%if (Size.getNodeName().equals(str)){ %>
<td><a name="testa" href="javascript:showVideo('<%= Size.getAttributes().getNamedItem("file").getNodeValue() %>')" class="ind">
<%= Size.getFirstChild().getNodeValue() 	%>

<% } %>


<% } %>

</a>	
<% }
%></td></tr>

<% } %>
</table></td></tr>
<!-------------------------太極拳總表--------------------------------------------------------------->
<tr id="AllList_T" class="off"><td><table width="100%" border="1" align="center">
<% for(int i=0;i<MethodNodes.getLength();i++){ 
Node Method = MethodNodes.item(i);
if (isTextNode(Method))
continue;
%>

<tr><td  class="TEXT_STYLE"><%= Method.getNodeName() %></td>

<%
NodeList  SizeNodes= Method.getChildNodes();
for(int j=0;j<SizeNodes.getLength();j++){

Node Size = SizeNodes.item(j);
if (isTextNode(Size))
continue;
%>

<td><a name="testa" href="javascript:showVideo('<%= Size.getAttributes().getNamedItem("file").getNodeValue() %>')" class="ind">
<%= Size.getFirstChild().getNodeValue() %>
</a>	
<% }
%></td></tr>

<% } %>

</table></td></tr>
<!------------------------------------------------------------------------------------------------------------------------------------------->

<% } %>
</table>


										
											</td>
										</tr>
										<tr><center>
											<td align="center"><input  class="button" type="button" value="回到主選單" onClick="location='main.jsp'"/>
										
				<% if(rs.getRow()!=0){%><input  id="change" class="button" type="button" value="觀看運動總表" onClick="see('List_T','AllList_T','List_O','AllList_O')"></td>	
										<% } %>
										</center>
										</tr>
										
									</table>
								</td>
							</tr>
						</table>
					
					
					</td>
				</tr>
				<tr>
					<td><p>&nbsp;</p></td>
				</tr>
				<tr>
					<td bgcolor="#437A27"><div align="center" class="BOTTOM_STYLE">Copyright@ITRI and CCU_PLlab</div></td>
				
				</tr>
						
			</table>
		</td>
		<td width="5%">&nbsp;</td>
	</tr>			


</table>
<p>&nbsp;</p>





	 
	

</body>
</html>
